/**
 * 
 */
package com.gr.staffpm.tasks.service;

import java.util.Iterator;
import java.util.List;

import org.apache.wicket.extensions.markup.html.repeater.data.sort.ISortState;
import org.apache.wicket.extensions.markup.html.repeater.data.table.ISortableDataProvider;
import org.apache.wicket.extensions.markup.html.repeater.util.SingleSortState;
import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import com.gr.staffpm.datatypes.TaskQueue;
import com.gr.staffpm.datatypes.User;
import com.gr.staffpm.tasks.model.QueryUtils;
import com.gr.staffpm.widget.jqgrid.model.SortInfo;
import com.gr.staffpm.widget.jqgrid.model.SortOrder;


/**
 * @author Graham Rhodes 16 Jan 2011 16:22:47
 */
public class SortableTaskQueueDataProvider extends ListDataProvider<TaskQueue> implements ISortableDataProvider<TaskQueue> {

    private static final long serialVersionUID = 1L;
    private SingleSortState state = new SingleSortState();
    private final TaskQueueService taskQueueService;
    private List<TaskQueue> queuedTasks;
    private final User manager;

    public SortableTaskQueueDataProvider(TaskQueueService taskQueueService, User manager) {
        //Set default sort
        state.setSort(new SortParam(TaskQueue.CREATED, true));
        this.taskQueueService = taskQueueService;
        this.manager = manager;
        updateQueuedTasks();
    }

    /**
     * @see org.apache.wicket.markup.repeater.data.IDataProvider#iterator(int, int)
     */
    @Override
    public Iterator<? extends TaskQueue> iterator(int first, int count) {
        if (this.state == null || this.state.getSort() == null) {
            return super.iterator(first, count);
        } else {
            SortParam sort = this.state.getSort();
            SortInfo sortInfo = new SortInfo(sort.getProperty(), sort.isAscending() ? SortOrder.asc : SortOrder.desc);
            if (sortInfo != null)
                QueryUtils.sortList(queuedTasks, sortInfo);
            int toIndex = first + count;
            if (toIndex > queuedTasks.size()) {
                toIndex = queuedTasks.size();
            }
            return queuedTasks.subList(first, toIndex).listIterator();
        }
    }

    /* (non-Javadoc)
     * @see org.apache.wicket.extensions.markup.html.repeater.data.sort.ISortStateLocator#getSortState()
     */
    @Override
    public ISortState getSortState() {
        return state;
    }

    /* (non-Javadoc)
     * @see org.apache.wicket.extensions.markup.html.repeater.data.sort.ISortStateLocator#setSortState(org.apache.wicket.extensions.markup.html.repeater.data.sort.ISortState)
     */
    @Override
    public void setSortState(ISortState state) {
        this.state = (SingleSortState) state;
    }

    public void updateQueuedTasks() {
        this.queuedTasks = taskQueueService.getQueuedTasksForManager(manager);
    }
}
